package com.ruoyi.api.wsmessage.controller;

import java.util.*;

import com.ruoyi.api.wsmessage.domain.MsgVo;
import com.ruoyi.result.ResultView;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.api.wsmessage.domain.Wsmessage;
import com.ruoyi.api.wsmessage.service.IWsmessageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 万顺消息Controller
 *
 * @author xzshan
 * @date 2020-06-16
 */
@Controller
@RequestMapping("/api/wsmessage")
public class WsmessageController extends BaseController {
  private String prefix = "api/wsmessage";

  @Autowired
  private IWsmessageService wsmessageService;

  @RequiresPermissions("api:wsmessage:view")
  @GetMapping()
  public String wsmessage() {
    return prefix + "/wsmessage";
  }

  /**
   * 查询万顺消息列表
   */
//  @RequiresPermissions("api:wsmessage:list")
  @PostMapping("/list/{loginName}/{state}")
  @ResponseBody
  public ResultView list(@PathVariable String loginName, @PathVariable Long state) {
    Wsmessage wsmessage = new Wsmessage();
    wsmessage.setUserReceiverLoginName(loginName);
    wsmessage.setState(state);
    List<Wsmessage> list = wsmessageService.selectWsmessageList(wsmessage);

    List<MsgVo> listVo = new ArrayList<>();

    List<Wsmessage> dailyWorkList = new ArrayList<>();
    MsgVo msgVo1 = new MsgVo();
    msgVo1.setType("dailyWork");
    msgVo1.setList(dailyWorkList);
    listVo.add(msgVo1);

    List<Wsmessage> applyList = new ArrayList<>();
    MsgVo msgVo2 = new MsgVo();
    msgVo2.setType("apply");
    msgVo2.setList(applyList);
    listVo.add(msgVo2);

    for(Wsmessage wsMsg:list){
      String type = wsMsg.getType();
      if(type!=null && (type.equalsIgnoreCase("pm")||type.equalsIgnoreCase("am")||type.equalsIgnoreCase("driverreport"))){
        dailyWorkList.add(wsMsg);
      }else if(type!=null && type.equalsIgnoreCase("maintain") ||type.equalsIgnoreCase("applyassets")){
        applyList.add(wsMsg);
      }
    }
    return ResultView.ok(listVo);
  }

  /**
   * 获取某个模块下所有已读和未读的消息
   */
  @PostMapping("/listAllByType/{loginName}/{type}")
  @ResponseBody
  public ResultView listAllByType(@PathVariable String loginName, @PathVariable String type) {
    Wsmessage wsmessage = new Wsmessage();
    wsmessage.setUserReceiverLoginName(loginName);
    if(type.equalsIgnoreCase("dailyWork")){
      StringBuilder sb=new StringBuilder();
      sb.append("am").append(",").append("pm").append(",driverreport");
      String type1=sb.toString();
      wsmessage.setType(type1);
      sb=null;
    }else if(type.equalsIgnoreCase("apply")){
      StringBuilder sb=new StringBuilder();
      sb.append("maintain").append(",applyassets");
      String type1=sb.toString();
      wsmessage.setType(type1);
    }
    List<Wsmessage> list = wsmessageService.listAllByType(wsmessage);

    MsgVo msgVo1 = new MsgVo();
    msgVo1.setType(type);
    msgVo1.setList(list);

    return ResultView.ok(msgVo1);
  }




  /**
   * 批量修改为已读
   */
  @Log(title = "万顺消息", businessType = BusinessType.UPDATE)
  @PostMapping("/updateStateBatch/{loginName}")
  @ResponseBody
  public ResultView updateStateBatch(@RequestParam String ids, @PathVariable String loginName) {
    long rows = wsmessageService.updateWsmessageStateByIds(ids);
    return ResultView.ok(rows);
  }


  /**
   * 导出万顺消息列表
   */
  @RequiresPermissions("api:wsmessage:export")
  @Log(title = "万顺消息", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(Wsmessage wsmessage) {
    List<Wsmessage> list = wsmessageService.selectWsmessageList(wsmessage);
    ExcelUtil<Wsmessage> util = new ExcelUtil<Wsmessage>(Wsmessage.class);
    return util.exportExcel(list, "wsmessage");
  }

  /**
   * 新增万顺消息
   */
  @GetMapping("/add")
  public String add() {
    return prefix + "/add";
  }

  /**
   * 新增保存万顺消息
   */
  @RequiresPermissions("api:wsmessage:add")
  @Log(title = "万顺消息", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(Wsmessage wsmessage) {
    return toAjax(wsmessageService.insertWsmessage(wsmessage));
  }

  /**
   * 修改万顺消息
   */
  @GetMapping("/edit/{id}")
  public String edit(@PathVariable("id") Long id, ModelMap mmap) {
    Wsmessage wsmessage = wsmessageService.selectWsmessageById(id);
    mmap.put("wsmessage", wsmessage);
    return prefix + "/edit";
  }

  /**
   * 修改保存万顺消息
   */
  @RequiresPermissions("api:wsmessage:edit")
  @Log(title = "万顺消息", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(Wsmessage wsmessage) {
    return toAjax(wsmessageService.updateWsmessage(wsmessage));
  }

  /**
   * 删除万顺消息
   */
  @Log(title = "万顺消息", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public ResultView remove(String ids) {
    int i = wsmessageService.deleteWsmessageByIds(ids);
    return ResultView.ok(i);
  }



}
